from django.db import models
from film.models import Film


class Area(models.Model):
    """城市区域模型 - 基于hotCity.json数据结构"""
    area_id = models.IntegerField(unique=True, primary_key=True, verbose_name="区域ID")
    parent_id = models.IntegerField(default=0, verbose_name="父级区域ID")
    area_name = models.CharField(max_length=100, verbose_name="区域名称")
    area_type = models.IntegerField(default=1, verbose_name="区域类型",
                                    help_text="1:省份, 2:城市, 3:区县")
    agency_id = models.IntegerField(default=0, verbose_name="代理ID")
    son_id = models.TextField(blank=True, verbose_name="子区域ID列表",
                              help_text="逗号分隔的子区域ID字符串")
    sort = models.IntegerField(default=0, verbose_name="排序权重")
    hot = models.IntegerField(default=False, verbose_name="是否热门城市")
    pinyin = models.CharField(max_length=100, blank=True, verbose_name="拼音")

    class Meta:
        db_table = 'area'
        verbose_name = '城市区域'
        verbose_name_plural = '城市区域'
        ordering = ['-hot', 'sort', 'area_id']


class Cinema(models.Model):
    """影院模型"""
    store_id = models.IntegerField(unique=True, verbose_name="影院ID")
    # film = models.ForeignKey(Film, default=1, on_delete=models.CASCADE, verbose_name="关联电影")
    type = models.IntegerField(default=2, verbose_name="类型")
    cat_id = models.IntegerField(default=0, verbose_name="分类ID")
    area_id = models.IntegerField(default=0, verbose_name="区域ID")
    seat = models.TextField(default='', blank=True, null=True, verbose_name="影院座位数据")
    img = models.CharField(max_length=255, blank=True, verbose_name="主图")
    imgs = models.CharField(max_length=255, blank=True, verbose_name="图片集")
    title = models.CharField(max_length=255, verbose_name="影院名称")
    comment_count = models.IntegerField(default=0, verbose_name="评论数量")
    comment_bad_count = models.IntegerField(default=0, verbose_name="差评数量")
    comment_good_count = models.IntegerField(default=0, verbose_name="好评数量")
    sell_count = models.IntegerField(default=0, verbose_name="销售数量")
    work_time = models.CharField(max_length=255, blank=True, verbose_name="营业时间")
    tel = models.CharField(max_length=50, blank=True, verbose_name="联系电话")
    address = models.CharField(max_length=500, verbose_name="地址")
    lat = models.CharField(max_length=50, blank=True, verbose_name="纬度")
    lng = models.CharField(max_length=50, blank=True, verbose_name="经度")
    start_price = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name="起价")
    send_price = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name="配送费")
    avg_price = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name="平均价格")
    sort = models.IntegerField(default=50, verbose_name="排序")
    state = models.IntegerField(default=1, verbose_name="状态")
    description = models.TextField(blank=True, verbose_name="描述")
    created_at = models.CharField(max_length=50, blank=True, verbose_name="创建时间")
    updated_at = models.CharField(max_length=50, blank=True, verbose_name="更新时间")

    class Meta:
        db_table = 'cinema'
        verbose_name = '影院'
        verbose_name_plural = '影院'
        ordering = ['-sort', '-store_id']

    def __str__(self):
        return self.title

    def get_area_info(self):
        """获取影院所属区域信息"""
        try:
            return Area.objects.get(area_id=self.area_id)
        except Area.DoesNotExist:
            return None
